package com.hk.jmm.presenter;

import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.lifecycle.Lifecycle;

import com.hjq.toast.ToastUtils;
import com.hk.jmm.R;
import com.hk.jmm.app.RegisterActivity;
import com.hk.jmm.app.fragment.AiFragment;
import com.hk.jmm.model.AiModel;
import com.hk.jmm.model.RegisterModel;
import com.skyz.base.mvp.BasePresenter;
import com.skyz.wrap.utils.CountDownTimerUtils;

public class RegisterPresenter extends BasePresenter<RegisterModel, RegisterActivity> {

    public RegisterPresenter(RegisterActivity view, Lifecycle lifecycle) {
        super(view, lifecycle);
    }

    @Override
    protected RegisterModel initMvpModel() {
        return new RegisterModel();
    }

    CountDownTimerUtils countDownTimerUtils;
    public void checkNull() {
        String phone = getMvpView().et_phone.getText().toString();
        if (phone.isEmpty()){
            showBtnBg(false);
            return;
        }
        String code = getMvpView().et_code.getText().toString();
        if (code.isEmpty()){
            showBtnBg(false);
            return;
        }
        String pass = getMvpView().et_pass.getText().toString();
        if (pass.isEmpty()){
            showBtnBg(false);
            return;
        }
        String passAgain = getMvpView().et_pass_again.getText().toString();
        if (passAgain.isEmpty()){
            showBtnBg(false);
            return;
        }
        showBtnBg(true);
    }

    private void showBtnBg(boolean show){
        getMvpView().click_save.setBackgroundResource(show? R.drawable.login_btn:R.drawable.login_btn_none);
        getMvpView().click_save.setClickable(show);
    }

    public void sysShow(ImageView sysBtnShowImage, EditText editText) {
        TransformationMethod method = getMvpView().et_pass.getTransformationMethod();
        boolean isHid = method==null || method.equals(HideReturnsTransformationMethod.getInstance());
        if(isHid){
            sysBtnShowImage.setImageResource(R.mipmap.sys_btn_show);
            editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }else {
            sysBtnShowImage.setImageResource(R.mipmap.sys_btn_hide);
            editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
    }

    public void sendCode() {
        if(countDownTimerUtils==null){
            countDownTimerUtils =new CountDownTimerUtils( getMvpView().click_send_code);
        }
        String phone = getMvpView().et_phone.getText().toString();
        if(phone.length()!=11){
            ToastUtils.show("请输入正确的手机号");
            return;
        }
        countDownTimerUtils.start();
    }

    public void register() {
        String pass = getMvpView().et_pass.getText().toString();
        String passAgain = getMvpView().et_pass_again.getText().toString();
        if(!pass.equals(passAgain)){
            ToastUtils.show("两次密码输入不一致");
            return;
        }
    }

    public void onDestroy(){
        if(countDownTimerUtils!=null){
            countDownTimerUtils.cancel();
            countDownTimerUtils=null;
        }
    }
}
